package cuid.sc20.laboratory.controller;

import cuid.sc20.laboratory.annotation.Log;
import cuid.sc20.laboratory.common.AjaxResult;
import cuid.sc20.laboratory.common.BusinessType;
import cuid.sc20.laboratory.common.TableDataInfo;
import cuid.sc20.laboratory.domain.TbRolePermission;
import cuid.sc20.laboratory.service.ITbRolePermissionService;
import cuid.sc20.laboratory.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 角色权限对照Controller
 *
 * @author ruoyi
 * @date 2023-06-30
 */
@RestController
@RequestMapping("/system/role/permission")
public class TbRolePermissionController extends BaseController
{
    @Autowired
    private ITbRolePermissionService tbRolePermissionService;

    /**
     * 查询角色权限对照列表
     */
    @PreAuthorize("@ss.hasPermi('system:permission:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbRolePermission tbRolePermission)
    {
        startPage();
        List<TbRolePermission> list = tbRolePermissionService.selectTbRolePermissionList(tbRolePermission);
        return getDataTable(list);
    }

    /**
     * 导出角色权限对照列表
     */
    @PreAuthorize("@ss.hasPermi('system:permission:export')")
    @Log(title = "角色权限对照", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRolePermission tbRolePermission)
    {
        List<TbRolePermission> list = tbRolePermissionService.selectTbRolePermissionList(tbRolePermission);
        ExcelUtil<TbRolePermission> util = new ExcelUtil<TbRolePermission>(TbRolePermission.class);
        util.exportExcel(response, list, "角色权限对照数据");
    }

    /**
     * 获取角色权限对照详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:permission:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbRolePermissionService.selectTbRolePermissionById(id));
    }

    /**
     * 新增角色权限对照
     */
    @PreAuthorize("@ss.hasPermi('system:permission:add')")
    @Log(title = "角色权限对照", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRolePermission tbRolePermission)
    {
        return toAjax(tbRolePermissionService.insertTbRolePermission(tbRolePermission));
    }

    /**
     * 修改角色权限对照
     */
    @PreAuthorize("@ss.hasPermi('system:permission:edit')")
    @Log(title = "角色权限对照", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRolePermission tbRolePermission)
    {
        return toAjax(tbRolePermissionService.updateTbRolePermission(tbRolePermission));
    }

    /**
     * 删除角色权限对照
     */
    @PreAuthorize("@ss.hasPermi('system:permission:remove')")
    @Log(title = "角色权限对照", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbRolePermissionService.deleteTbRolePermissionByIds(ids));
    }
}
